package com.sure.supermarketserver.branch.information;

import com.sure.supermarketserver.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController//说明这是一个受spring容器管理的控制器组件(Bean)
@RequestMapping("/branch/information")
public class BranchAPI {

    @Resource//表示从容器中引入依赖对象
    private BranchService branchService;

    @GetMapping("")
    public Result branchList(BranchDto dto) {
        //dto会自动按照属性与参数名匹配的规则自动接收
        Map<String, Object> page = branchService.getBranchList(dto);
        return Result.success(page);
    }

    @RequestMapping("")
    public Result branchAdd(@RequestBody BranchDto dto) {
        try {
            branchService.addBranch(dto);
        } catch (Exception e) {
            return Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage());
        }
        return Result.success();
    }

    @PutMapping("")
    public Result branchUpd(@RequestBody BranchDto dto) {
        branchService.updBranch(dto);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result branchDel(@PathVariable String id) {
        try {
            branchService.delBranch(id);
        } catch (Exception e) {
            return Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage());
        }
        return Result.success();
    }

    @GetMapping("/brst")
    public Result brstList(StaffDto dto) {
        //dto会自动按照属性与参数名匹配的规则自动接收
        Map<String, Object> page = branchService.getBrst(dto);
        return Result.success(page);
    }
}
